package cn.wolfcode.rbac.controller;

import cn.wolfcode.rbac.exception.CustomException;
import cn.wolfcode.rbac.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Lu.F
 * @version 1.0
 * @Description TODO
 * @date 2022/5/21 11:09
 */
@Controller
public class LoginController {

    @Autowired
    private EmployeeService employeeService;

    //登录页面请求
    @RequestMapping("/")
    public String showLogin() {
        return "login";
    }


    //登录请求
    @RequestMapping("/login")
    public String login(String username, String password, Model model) {
        try {
            employeeService.login(username, password);
            return "redirect:department/list";
        } catch (CustomException e) {
            e.printStackTrace();
            model.addAttribute("errorMsg", e.getMsg());
            return "/";
        }
    }

    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping("/logout")
    public String logout() {
        employeeService.logout();
        return "redirect:/";
    }

    /**
     * 无权限访问
     *
     * @return
     */
    @RequestMapping("/no_permission")
    public String noPermission() {
        return "no_permission";
    }
}
